using System.Drawing;
using Microsoft.Win32;

namespace LeiRay
{
    internal class LeiRayConfiguration
    {
        private static readonly string LeiRayRegKey = Registry.CurrentUser.Name + "\\Software\\LeiRay";
        private const string DirectoryRegKeyName = "Directory";
        private static readonly string CurrentSizeHeightRegKeyName = "SizeHeight";
        private static readonly string CurrentSizeWidthRegKeyName = "SizeWidth";
        private static readonly string CurrentLocationXRegKeyName = "LocationX";
        private static readonly string CurrentLocationYRegKeyName = "LocationY";
        private const string DefaultDirectory = "C:\\";
        public string Directory
        {
            get
            {
                var value = (string) Registry.GetValue(LeiRayRegKey, DirectoryRegKeyName, DefaultDirectory);
                if(value == null)
                {
                    Registry.SetValue(LeiRayRegKey, DirectoryRegKeyName, DefaultDirectory);
                    value = DefaultDirectory;
                }
                return value;
            }
            set
            {
                Registry.SetValue(LeiRayRegKey, DirectoryRegKeyName, value);
            }
        }

        public Size Size
        {
            get
            {
                var height = (int?) Registry.GetValue(LeiRayRegKey, CurrentSizeHeightRegKeyName, null);
                var width = (int?) Registry.GetValue(LeiRayRegKey, CurrentSizeWidthRegKeyName, null);
                if (height != null && width != null)
                {
                    return new Size(width.Value, height.Value);
                }
                return Size.Empty;
            }
            set
            {
                Registry.SetValue(LeiRayRegKey, CurrentSizeHeightRegKeyName, value.Height, RegistryValueKind.DWord);
                Registry.SetValue(LeiRayRegKey, CurrentSizeWidthRegKeyName, value.Width, RegistryValueKind.DWord);
            }
        }

        public Point Location
        {
            get
            {
                var locationX = (int?) Registry.GetValue(LeiRayRegKey, CurrentLocationXRegKeyName, null);
                var locationY = (int?) Registry.GetValue(LeiRayRegKey, CurrentLocationYRegKeyName, null);
                if(locationX != null && locationY != null)
                {
                    return new Point(locationX.Value, locationY.Value);    
                }
                return Point.Empty;
            }
            set
            {
                Registry.SetValue(LeiRayRegKey, CurrentLocationXRegKeyName, value.X, RegistryValueKind.DWord);
                Registry.SetValue(LeiRayRegKey, CurrentLocationYRegKeyName, value.Y, RegistryValueKind.DWord);
            }
        }
    }
}